package example.com.terry.servlet;

import example.com.terry.utils.CookieUtils;
import example.com.terry.utils.JDBCUtils;
import example.com.terry.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: QueryWorkServlet <br/>
 * Description: 公告增加接口 <br/>
 * Date: 2020/5/21 下午5:27 <br/>
 * <br/>
 *
 * @author ls
 * <p/>
 * 修改记录
 * @version 1.0<br />
 */
@WebServlet("/insertNoticServlet")
public class InsertNoticServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        if (CookieUtils.isSign(req)) {
            String id = req.getParameter("id");
            String noticeText = req.getParameter("noticeText");
            String type = req.getParameter("type");
            if (id != null) {

                String sql = "update tb_notice set notice_text='" + noticeText + "', notice_type='" + type + "'  where id = " + id;
                if (JDBCUtils.init(sql)) {
                    resp.getWriter().println("修改成功");
                } else {
                    resp.getWriter().println("修改失败");
                }

            } else {

                if (StringUtils.isNotBlank(noticeText)) {
                    if ("admin".equals(CookieUtils.getUser(req))) {
                        String sql = "insert tb_notice(notice_text,notice_type,notice_man,create_time) values('" + noticeText + "','" + type + "','" + CookieUtils.getUser(req) + "',now())";
                        if (JDBCUtils.init(sql)) {
                            resp.getWriter().println("公告增加成功");
                        } else {
                            resp.getWriter().println("公告增加失败");
                        }

                    } else {
                        resp.getWriter().println("当前用户非admin用户不可更改公告");
                    }
                } else {
                    resp.getWriter().println("当前公告内容为空");
                }
            }

        } else {
            // todo 没登录
            resp.getWriter().println("当前非登陆状态请重新登陆");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);

        }

    }
}
